package com.example.bbs.dao.impl;


import com.example.bbs.dao.BaseDao;
import com.example.bbs.dao.ReplyDao;
import com.example.bbs.model.Reply;
import com.example.bbs.utils.TimeUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


/**
 * @auther: zhangxin
 * @Description:
 */
public class ReplyDaoImpl extends BaseDao implements ReplyDao {


    @Override
    public List<Reply> findByPostId(int postId) {
        List<Reply> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement("select * from reply where post_id = ?");
            //填坑
            statement.setInt(1, postId);

            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                list.add(new Reply(resultSet.getInt(1),resultSet.getInt(2),resultSet.getInt(3),resultSet.getString(4), TimeUtil.getStringToDate(resultSet.getString(5))));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return list;
    }

    @Override
    public boolean addReply(Reply reply) {

        String sql = " insert into reply values(default,?,?,?,?)";

        return executeUpdate(sql,reply.getPostId(),reply.getUserId(),reply.getContent(),reply.getReplyTime());
    }

    @Override
    public List<Reply> findByUserId(int userId) {
        List<Reply> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //2.创建连接对象
            connection = getConnection();
            //3.创建预处理执行对象
            statement = connection.prepareStatement("select * from reply where post_id in (select post_id from post where user_id = ?)");
            //填坑
            statement.setInt(1, userId);

            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                list.add(new Reply(resultSet.getInt(1),resultSet.getInt(2),resultSet.getInt(3),resultSet.getString(4), TimeUtil.getStringToDate(resultSet.getString(5))));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement, resultSet);
        }
        return list;
    }

    public boolean deleteReplyById(int replyId){

        String sql = "delete from reply where reply_id = ?";
        return executeUpdate(sql,replyId);
    }

    public boolean updateReplyById(Reply reply){

        String sql = "update reply set reply_content = ? where reply_id = ?";
        return executeUpdate(sql,reply.getContent(),reply.getReplyId());
    }
}
